<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "fuxue".
 *
 * @property string $stu_id
 * @property string $date
 * @property string $grade
 * @property string $class
 * @property string $reason
 */
class FuxueModel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'fuxue';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['stu_id','name'], 'required'],
            [['stu_id', 'date', 'grade','name', 'class'], 'string', 'max' => 50],
            [['reason'], 'string', 'max' => 500],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'stu_id' => '学号',
            'date' => '申请日期',
            'grade' => '编入年级',
            'class' => '编入班级',
            'reason' => '申请原因',
        		'name' => '姓名',
        ];
    }

    /**
     * 保存导入excel文件内容
     * @param $data  传入文件内容的数组
     * @return bool  保存成功返回true
     *
     */
    public static function SaveImport($data){

        try{

            foreach ($data as $value){

                $person=new FuxueModel();
                $person->stu_id=$value['学号'].'';//学号要转化为字符型才能通过验证
                $person->name=$value['姓名'].'';
                $person->date=$value['申请日期'].'';
                $person-> grade=$value['编入年级'].'';
                $person->class=$value['编入班级'].'';
                $person->reason=$value['申请原因'].'';

                $person->save();
            }
            return true;

        }catch (\Exception $e){
            throw $e;
        }

    }


}
